package com.right.health.app.ui.main;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

import com.hyphenate.easeui.ui.EaseConversationListFragment;
import com.magic.basic.utils.PermissionUtil;
import com.magic.basic.utils.ToastUtil;
import com.right.health.app.R;
import com.right.health.app.app.App;
import com.right.health.app.base.BasePresenterActivity;
import com.right.health.app.push.TagAliasOperatorHelper;
import com.right.health.app.ui.main.fragment.ConsultFragment;
import com.right.health.app.ui.main.fragment.HomeFragment;
import com.right.health.app.ui.main.fragment.HouseholdFragment;
import com.right.health.app.ui.main.fragment.MeFragment;
import com.right.health.app.ui.main.fragment.MedicalFragment;
import com.right.health.app.ui.user.LoginActivity;

import static com.right.health.app.push.TagAliasOperatorHelper.ACTION_DELETE;

/*
 * Created by hopeliao on 2019-09-23
 *
 */
public class MainActivity extends BasePresenterActivity implements View.OnClickListener {
    private static final String INTENT_REQUEST_TAB = "requestTab";
    private static final int MAX_CHILD_SIZE = 5;
    private static final String SAVE_FRAGMENT_KEY = "currentIndex_key";

    /**
     * 首页
     */
    public static final int TAB_HOME = 0;
    /**
     * 家居
     */
    public static final int TAB_HOUSEHOLD = 1;
    /**
     * 咨询
     */
    private static final int TAB_ADVISOY = 2;
    /**
     * 医疗
     */
    public static final int TAB_MEDICAL = 3;
    /**
     * 我的
     */
    private static final int TAB_ME = 4;

    private View[] mBottomViews;

    private Fragment mFragment;

    private int mCurIndex=0;

    @Override
    public void onCreate(Bundle bundle, String s) {

        if (bundle != null) {
            mCurIndex = bundle.getInt(SAVE_FRAGMENT_KEY, 0);
        }
        setContentView(R.layout.main);
    }

    @Override
    public void findView() {
        mBottomViews = new View[MAX_CHILD_SIZE];
        mBottomViews[0] = findViewById(R.id.home_btn);
        mBottomViews[1] = findViewById(R.id.household_btn);
        mBottomViews[2] = findViewById(R.id.advisory_btn);
        mBottomViews[3] = findViewById(R.id.medical_btn);
        mBottomViews[4] = findViewById(R.id.me_btn);


        for (View childView : mBottomViews) {
            childView.setOnClickListener(this);
        }
    }

    @Override
    public void initialize() {
        replaceView(mCurIndex);
        PermissionUtil.with(MainActivity.this)
                .listener((succ, requestCode) -> {
                    if (succ) {

                    } else {
                        new AlertDialog.Builder(MainActivity.this)
                                .setMessage("获取权限失败")
                                .setPositiveButton(R.string.yes, null).show();
                    }
                }).request(Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.CAMERA,
                Manifest.permission.RECORD_AUDIO,
                Manifest.permission.VIBRATE);
    }

    @Override
    public void onClick(View v) {
        for (int i = 0; i < MAX_CHILD_SIZE; i++) {
            View view = mBottomViews[i];
            if(view == v) {
                replaceView(i);
            }
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Bundle bundle = intent.getExtras();
        if(bundle != null && bundle.containsKey(INTENT_REQUEST_TAB)) {
            int requestTab = bundle.getInt(INTENT_REQUEST_TAB);
            replaceView(requestTab);
        }
    }

    public static void makeActivity(Activity act, int requestTab) {
        Intent intent = new Intent(act, MainActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt(INTENT_REQUEST_TAB, requestTab);
        intent.putExtras(bundle);

        act.startActivity(intent);
    }

    /**
     * 切换视图
     *
     * @param currItem 当前TAB
     */
    public synchronized void replaceView(int currItem) {
            if(!App.getInstance().isLogined()) {
                startIntent(LoginActivity.class);
                overridePendingTransition(0,0);
                TagAliasOperatorHelper.sequence++;
                TagAliasOperatorHelper.TagAliasBean delBean = new TagAliasOperatorHelper.TagAliasBean();
                delBean.action = ACTION_DELETE;
                delBean.isAliasAction = true;
                TagAliasOperatorHelper.getInstance().handleAction(this,  TagAliasOperatorHelper.sequence, delBean);
                ToastUtil.show(MainActivity.this, "请先登录");

                return;
            }

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        if (mFragment != null) {
            transaction.hide(mFragment);
        }

        for (View view : mBottomViews) {
            view.setSelected(false);
        }
        mBottomViews[currItem].setSelected(true);
        mCurIndex = currItem;
        if (getSupportFragmentManager().findFragmentByTag(currItem+"")==null){

            Class<? extends Fragment> clazz = null;
            switch (currItem) {
                case TAB_HOME:
                    clazz = HomeFragment.class;
                    break;
                case TAB_HOUSEHOLD:
                    clazz = HouseholdFragment.class;
                    break;
                case TAB_ADVISOY:
                    clazz = ConsultFragment.class;
                    break;
                case TAB_MEDICAL:
                    clazz = MedicalFragment.class;
                    break;
                case TAB_ME:
                    clazz = MeFragment.class;
                    break;
                default:
                    break;
            }

            if( clazz != null) {
                try {
                    Fragment fragment = clazz.newInstance();
                    transaction.add(R.id.frameLayout, fragment,currItem+"");
                    mFragment = fragment;
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                }
            }
        }else {
            mFragment=getSupportFragmentManager().findFragmentByTag(currItem+"");
            transaction.show(mFragment);
        }
        transaction.commit();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putInt(SAVE_FRAGMENT_KEY, mCurIndex);
        super.onSaveInstanceState(outState);
    }
}
